

$(document).ready(function(){
    
    var options = { 
        dataType:      'json',
        beforeSubmit:  showRequest,
        success:       showResponse
    }; 
    $('#new_campaign').validationEngine('attach');
    $('#new_campaign').ajaxForm(options);
    
    
    //
    my_campaign_energy_chart.campaign_actions();
    my_campaign_energy_chart.drag_energy_chart();
    my_campaign_energy_chart.campaign_btn_actions();
    
    $(".energy_chart").popover({
        html : true, 
        content: function() {
            var dataID = $(this).attr("data-id");
            return $('#ec_td_val_' + dataID).html();
        }
    }).css(".popover-title", "background:red");
    $('.energy_chart,.br_s_tab').on('click', function (e) {
        $('.energy_chart').not(this).popover('hide');
    });
    
    var json = {};
    json['action_type'] = 'campaign_planner';
    getFunctions.selectOne(json);
    getFunctions.selectAll(json);
    
})


my_campaign_energy_chart  = {

    drag_energy_chart : function(){
        
        $("tbody.drag_energy_chart").sortable({
            connectWith: ".drag_energy_chart",
            //items: "> tr:not(:first)",
            //appendTo: $tabs,
            helper: function(e, tr)
            {
                    var $originals = tr.children();
                    var $helper = tr.clone();
                    $helper.children().each(function(index)
                    {
                      // Set helper cell sizes to match the original sizes
                      $(this).width($originals.eq(index).width())
                    });
                    $('.energy_chart').not(this).popover('hide');
                    return $helper;
            },
            zIndex: 999990
        })
        .disableSelection()
    },

    campaign_btn_actions : function(){
        $("body").on("click",".trigger_campaign_btn",function(){
            var data_type = $(this).attr("data-type");
            var data_quarter = $(this).attr("data-quarter");
            
            var get_all_tr = $(".td_arr_sorting").map(function(){
                if (data_quarter == $(this).attr("data-type")) {
                    return $(this).attr("data-td-arr");
                }
            }).get().join(",");
            
            switch (data_type){
                case "confirm":
                    var data_action = $(this).attr("data-action");
                    var dataID = $("body").data(data_action);
                    var split_data = dataID.split(",");
                    jConfirm( 'Approve Campaign', 'Are you sure you want to approve selected campaign?','', '', '', function(e){
                        if (e == true) {
                            $.ajax({
                                type : "POST",
                                url  : "/ajax/energy-chart-actions",
                                data : {action:"approved_selected_campaign",dataID:dataID},
                                success : function(data){
                                    var data_json = JSON.parse(data);
                                    
                                    if (data_json['notification'] == "Done") {
                                    
                                        $(split_data).each(function(e,val){
                                            $("#camp_" + val).animate({backgroundColor: "whiteSmoke"}, "fast")
                                            .animate({opacity: "hide"}, "slow");
                                        });
                                        
                                        
                                        notify_s('Success', data_json['msg']);
                                    }
                                    
                                    
                                }
                            });
                        }
                    });
                    break;
                case "return":
                    var data_action = $(this).attr("data-action");
                    
                    var dataID = $("body").data(data_action);
                    
                    jConfirm( 'Move Campaign', 'Are you sure you want to move selected campaign?','', '', '', function(e){
                        if (e == true) {
                            
                            var json = {};
                            
                            var split_dataID = dataID.split(",");
                            
                            $(split_dataID).each(function(e,val){
                                
                                var data_json = $("#camp_" + val).attr("data-json");
                                var data_json_parse = JSON.parse(data_json);
                                json[val] = parseInt(data_json_parse['campaign_id']) + 1;
                            })
                            
                            var next_campaign = JSON.stringify(json);
                            
                            $.ajax({
                                type : "POST",
                                url  : "/ajax/energy-chart-actions",
                                data : {action:"move_selected_campaign",next_campaign:next_campaign},
                                success : function(data){
                                    var data_json = JSON.parse(data);
                                    
                                    if (data_json['notification'] == "Done") {
                                        
                                        notify_s('Success', data_json['msg']);
                                    }
                                    
                                    
                                }
                            });
                        }
                    });
                    break;
            
                case "completed":
                    jConfirm( 'Confirmation', 'This will be tagged as completed, Users may still edit the completed energy chart. Are you sure you want to proceed?','', '', '', function(e){
                        if (e == true) {
                        //console.log(get_all_tr)
                            $.ajax({
                                type : "POST",
                                url  : "/ajax/energy-chart-actions",
                                data : {action:"complete_quarter_campaign",get_all_tr:get_all_tr,data_quarter:data_quarter},
                                success : function(data){
                                    var data_json = JSON.parse(data);
                                    
                                    if (data_json['notification'] == "Done") {
                                        
                                        notify_s('Success', data_json['msg']);
                                    }
                                    
                                    
                                }
                            });
                        }
                    });
                break
                
                case "final":
                    jConfirm( 'Confirmation', 'This will be tagged as final and will no longer be editable, are you sure you want to proceed?','', '', '', function(e){
                        if (e == true) {
                        //console.log(get_all_tr)
                            $.ajax({
                                type : "POST",
                                url  : "/ajax/energy-chart-actions",
                                data : {action:"final_quarter_campaign",get_all_tr:get_all_tr,data_quarter:data_quarter},
                                success : function(data){
                                console.log(data)
                                    var data_json = JSON.parse(data);
                                    
                                    if (data_json['notification'] == "Done") {
                                        
                                        notify_s('Success', data_json['msg']);
                                        $("#drag_ec_" + data_quarter).removeClass('drag_energy_chart'); 
                                        $("#tbl_ec_" + data_quarter).toggleClass("tb_brochure tb_brochure_final");
                                        $("#tbl_ec_actions_" + data_quarter).hide();
                                        
                                        $(".ec_val_" + data_quarter).each(function(){
                                            var get_val = $(this).html();
                                            $(this).parent().html(get_val);
                                        })
                                    }
                                    
                                    
                                }
                            });
                        }
                    });
            }
            
            
        });
    },
    
    submit_ec_save : function(){
        $("body").on("click",".submit_ec_save",function(){
           
                jConfirm( 'Confirmation', 'Editing completed Energy Chart will create another version, Are you sure you want to proceed?','', '', '', function(e){
                    
                    if (e == true) {
                        $("#submit").submit();
                    }
                });
                
            
        });
    },
    
    campaign_actions : function(){
    
        my_campaign_energy_chart.submit_ec_save();
        
        $("body").on("click",".trigger_campaign",function(){
        
            var data_type = $(this).attr("data-type");
            var dataID = $(this).parent().attr("data-id");
            var data_json = $(this).parent().parent().attr("data-json");
            var data_json_parse = JSON.parse(data_json);
            //console.log(data_json_parse);
            //console.log(dataID)
            // Campaign Actions
            switch (data_type) {
                case "confirm":
                    jConfirm( 'Approve Campaign', 'Are you sure you want to approve this?','', '', '', function(e){
                        if (e == true) {
                            $.ajax({
                                type : "POST",
                                url  : "/ajax/energy-chart-actions",
                                data : {action:"approved_campaign",dataID:dataID},
                                success : function(data){
                                    var data_json = JSON.parse(data);
                                    
                                    if (data_json['notification'] == "Done") {
                                        $("#camp_" + dataID).animate({backgroundColor: "whiteSmoke"}, "fast")
                                        .animate({opacity: "hide"}, "slow");
                                        
                                        notify_s('Success', data_json['msg']);
                                    }
                                    
                                    
                                }
                            });
                        }
                    });
                    break;
                case "return":
                    jConfirm( 'Move Campaign', 'Are you sure you want to move this?','', '', '', function(e){
                        if (e == true) {
                            $.ajax({
                                type : "POST",
                                url  : "/ajax/energy-chart-actions",
                                data : {action:"move_campaign",dataID:dataID,next_campaign:parseInt(data_json_parse['campaign_id']) + 1},
                                success : function(data){
                                    var data_json = JSON.parse(data);
                                    
                                    if (data_json['notification'] == "Done") {
                                        
                                        notify_s('Success', data_json['msg']);
                                    }
                                    
                                    
                                }
                            });
                        }
                    });
                    break;
                case "edit":
                    var toggle = $(this).attr("data-toggle");
                    var draft = $(this).parent().attr("data-draft");
                    // return value to the specific fields
                        $.each(data_json_parse,function(e,val){
                            if (draft == "3"){
                            
                                if (val == "3" && e == "campaign_status") {
                                    $("[name='" + e + "']").val("1");
                                }else{
                                    $("[name='" + e + "']").val(val);
                                    $(":checkbox[name='" + e + "']").attr("checked",true);
                                    $(":checkbox[name='" + e + "']").parent().addClass("checked");
                                    $("#sad_draft").show();
                                }
                            }else if(draft == "2"){
                                $("[name='" + e + "']").val(val);
                                $(":checkbox[name='" + e + "']").attr("checked",false);
                                $(":checkbox[name='" + e + "']").parent().removeClass("checked");
                                $("#sad_draft").hide();
                            }else{
                                $("[name='" + e + "']").val(val);
                                $(":checkbox[name='" + e + "']").attr("checked",false);
                                $(":checkbox[name='" + e + "']").parent().removeClass("checked");
                                $("#sad_draft").show();
                            }
                            
                        })
                    if (!toggle) {
                        $("#new_campaign").attr("action","/ajax/energy-chart-actions?action=update_campaign&type=edit&encrypt_id=" + dataID);
                    }else{
                        var data_quarter = $(this).attr("data-id");
                        var split_dataID = data_quarter.split("_");
                        $('input[name="quarter"]').val(split_dataID[2])
                        $("#new_campaign").attr("action","/ajax/energy-chart-actions?action=new_campaign_version&type=new&encrypt_id=" + dataID);
                    }
                    
                    break;
                    
                case "remove":
                
                    jConfirm( 'Remove Campaign', 'Are you sure you want to delete this?','', '', '', function(e){
                        if (e == true) {
                            
                            $.ajax({
                                type : "POST",
                                url  : "/ajax/energy-chart-actions",
                                data : {action:"remove_campaign",dataID:dataID},
                                success : function(data){
                                    var data_json = JSON.parse(data);
                                    
                                    if (data_json['notification'] == "Done") {
                                        $("#camp_" + dataID).animate({backgroundColor: "whiteSmoke"}, "fast")
                                        .animate({opacity: "hide"}, "slow");
                                        
                                        notify_s('Success', data_json['msg']);
                                    }
                                    
                                    
                                }
                            });
                            
                        }
                    })
                    break;
                    
            }
          
          
        });
    }


}